<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>新闻列表</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 0;
            padding: 20px;
            background-color: #f5f5f5;
        }
        .news-item {
            background-color: white;
            padding: 20px;
            margin-bottom: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            cursor: pointer;
        }
    </style>
</head>
<body>
    <h1>新闻列表</h1>
    <div id="news-container"></div>
    <script>
        async function fetchNews() {
            try {
                const response = await fetch('/news');
                const newsList = await response.json();
                const newsContainer = document.getElementById('news-container');

                newsList.news.forEach(news => {
                    const newsItem = document.createElement('div');
                    newsItem.className = 'news-item';
                    newsItem.onclick = () => viewDetail(news.id);

                    const newsTitle = document.createElement('h2');
                    newsTitle.textContent = news.title;

                    const newsDescription = document.createElement('p');
                    newsDescription.textContent = news.description;

                    newsItem.appendChild(newsTitle);
                    newsItem.appendChild(newsDescription);
                    newsContainer.appendChild(newsItem);
                });
            } catch (error) {
                console.error('Error fetching news:', error);
            }
        }

        function viewDetail(newsId) {
            window.location.href = `news_detail.html?id=${newsId}`;
        }
        document.addEventListener('DOMContentLoaded', fetchNews);
    </script>
</body>
</html>
